package grex.genes;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
import grex.BNFException;
import grex.ErrorManager;
import grex.Prediction;
import grex.PredictionContainer;
public class CatVar extends Gene {
  private static final int NROFCILDREN=0;
  private int varNr=-1;
  public CatVar() throws BNFException{
    super(NROFCILDREN);

  }

  public synchronized void execute(Gene parent,PredictionContainer data,int mode) throws GeneException {
      this.parent = parent;         
      if(varNr==-1){          
          varNr=environment.getRandomCategoricalColumnIndex();    
      }
      for (Prediction p : data.values()) {
            p.setPrediction(p.getInstance()[varNr]);
        }
  }

  public void setVarNr(int varNr){
    this.varNr=varNr;
  }

  public int getVarNr() {
    if(varNr==-1)
     varNr=environment.getRandomCategoricalColumnIndex();
    return varNr;
  }

 public Object clone(){ ;
    CatVar newCategorialVariable;
    try{
      newCategorialVariable = new CatVar();
          newCategorialVariable.setVarNr(varNr);
          newCategorialVariable.setOptions(ops);
          newCategorialVariable.setEnvironment(environment);
          newCategorialVariable.setParent(parent);
    }catch(grex.BNFException e){
      ErrorManager.getInstance().reportError(e);
      return null;
    }
    return newCategorialVariable;
  }
  /** @todo Om endast enn kategori finns kommer denna att volja lika ofta somma alla andra sammtidigt */

  public String toString(){        
            if (varNr == -1) {
                varNr = environment.getRandomCategoricalColumnIndex();
            }
            return environment.getVariableNames()[varNr];
  }
}
